<?php
set_time_limit(0);
include_once (PATH_SQL . 'category_sql.php');
include_once (PATH_MODEL . 'category_model.php');
include_once (PATH_VIEW_ADMIN . 'category_view.php');
$oModelCat = new Model_Category();
$oViewCat = new View_Admin_CategoryManangement($oModelCat);
$oModelCat->setData('page_size',1);
$oModelCat->setData('pagegroup_size',5);
$oModelCat->setData('stylesheet',"paging");

$sAction = $_REQUEST['sAction'];
$oModelCat->setData('cate_dir', $_REQUEST['cate_dir'] );
$oUpload = new Upload($_FILES);
switch ($sAction) {
    /*case 'updatePosition':{    
    //    print_R($_POST);
        // $oModel->updatePosition();      
        break;    
    }*/
    case 'update_status': {
            $oModelCat->unsetData('product_category_id');
            $oModelCat->unsetData('product_category_status');
            $oModelCat->unsetData('product_category_statistics');
            $oModelCat->updatePosition();
            if (count($_POST['hotel_page_sort'] > 0)) {
                foreach ($_POST['hotel_page_sort'] as $sKey => $sValue) {
                    $oModelCat->setData('product_category_id', $sKey);
                    $oModelCat->setData('product_category_status', intval($_POST['product_category_status'][$sKey]));
                    $oModelCat->updateCategory();
                }
            }

            $oModelCat->unsetData('product_category_id');
            $oModelCat->unsetData('product_category_status');
            $oModelCat->unsetData('product_category_statistics');

            if (count($_POST['product_sort'] > 0)) {
                foreach ($_POST['product_sort'] as $sKey => $sValue) {
                    $oModelCat->setData('product_category_id', $sKey);
                    $oModelCat->setData('product_category_statistics', intval($_POST['product_category_statistics'][$sKey]));
                    $oModelCat->updateCategory();
                }
            }
            redir('?sMod=category_index');
            break;
        }
    case 'add_new_form': {
            $oModelCat->setData('parent_category_id', trim($_GET['parent_category_id']) );
            $oModelCat->setData('parent_category_name', trim($_GET['parent_category_name']) );
            $oModelCat->setData('current_level', $_GET['current_level'] );
            $oViewCat->displayNewCategoryIndex($oModelCat);
            break;
        }
    case 'do_add_new': {
            $check_exits=$oModelCat->check_exit_category_id($_POST['product_category_id']);

            if($check_exits!=false) {
                redir('?sMod=category_index&status=exits_id');
            }
            
            if($_POST['product_category_yahoo_id']=='') {
                unset($_POST['product_category_yahoo_id']);
            }

            $check_insert = $oModelCat->insertCategory();
            if ($check_insert) {
            //upload image process
                if ($oUpload->getFileName("product_category_image") != "") {
                    if ($oUpload->isAllowImageFile("product_category_image")) {
                        $ext = $oUpload->getFileExtension("product_category_image");
                        $fileUpload = $check_insert . "." . $ext;
                        if (!$oUpload->saveAs($fileUpload, PATH_UPLOAD . "category/","product_category_image", "over", 0777)) {
                            redir('?sMod=category_index&sStatus=uploadImageError');
                        } else {
                            $aUpadteImage['product_category_id'] = $check_insert;
                            $aUpadteImage['product_category_image'] = $fileUpload;
                            $oModelCat->updateCategoryIcon($aUpadteImage);
                        }
                    }
                }
                $sReturn = '?sMod=category_index&status=addSuccess';
                if($_POST['product_category_parent_id'] > 0)
                    $sReturn.= '&sAction=list_child&cat_id='.$_POST['product_category_parent_id'].'&cat_name='.$_POST['parent_category_name'].'&current_level='.$_POST['current_level'].'&cate_dir='.$_POST['cate_dir'];

                redir($sReturn);
            }else {
                $sErrorMsg =  $oModelCat->aLanguage['ALERT']['form_cannot_submit'] . $sErrorMsg;
                $oModelCat->setErrorMsg($sErrorMsg);
            }
            break;
        }
    case 'check_exits_id': {
            $id = $_REQUEST['checkid'];
            $oViewCat->resultXML($oModelCat, '', $oModelCat->check_exit_category_id($id));
            die();
            break;
        }
    case 'do_delete': {
            $id=$_GET['id'];
            $row=$oModelCat->getInfoCategory($id);
            $oModelCat->deleteImage($row['product_category_image']);
            $result = $oModelCat->deleteCategory($id);
            if(!$result) {
                redir('?sMod=category_index&status=delError');
            }else {
                redir('?sMod=category_index&status=delSuccess');
            }
            break;
        }
    case 'edit_form': {
           
            $id = $_GET['id'];
            $oModelCat->setData('parent_category_id', trim($_GET['parent_category_id']) );
            $oModelCat->setData('parent_category_name', trim($_GET['parent_category_name']) );
            
            $oModelCat->setData('current_level', $_GET['current_level'] );
            $oViewCat->displayEditCategoryIndex($oModelCat);
            $rowManager = $oModelCat->getInfoCategory($id);
            $oViewCat->displayInfoCat($rowManager,$oModelCat);

            $category_image_old = $rowManager['product_category_image'];
            $oModelCat->setData('product_category_image_old',$category_image_old);
            $oModelCat->setData('product_category_image_old_display',$oModelCat->getThumbImage($category_image_old,20,20));
            break;
        }
    case 'do_edit': {
            $oModelCat->setData('product_category_id',$_POST['product_category_id']);
            if ($_POST['product_category_status'] == 'on')
                $oModelCat->setData('product_category_status', 1);
            else
                $oModelCat->setData('product_category_status', 0);

            $result=$oModelCat->updateCategory();

            if(!$result) {
                $sErrorMsg =  $oModelCat->aLanguage['ALERT']['form_cannot_submit'] . $sErrorMsg;
                $oModelCat->setErrorMsg($sErrorMsg);
            }
            else {
                $category_id=$oModelCat->getData('product_category_id');
                //upload image process
                if ($oUpload->getFileName("product_category_image") != "") {
                    if ($oUpload->isAllowImageFile("product_category_image")) {
                        $ext = $oUpload->getFileExtension("product_category_image");
                        $fileUpload = $category_id . "." . $ext;
                        //DELETE OLD
                        /*delete old image*/
                        $category_image_old = $oModelCat->getData('charge_service_image_old');
                        $oModelCat->deleteImage($category_image_old);
                        //UPLOAD NEW
                        if (!$oUpload->saveAs($fileUpload, PATH_UPLOAD . "category/","product_category_image", "over", 0777)) {
                            redir('?sMod=category_index&sStatus=uploadImageError');
                        } else {
                            $_SESSION['category_change_igm_id']=$category_id;
                            $aUpadteImage['product_category_id'] = $category_id;
                            $aUpadteImage['product_category_image'] = $fileUpload;
                            $oModelCat->updateCategoryIcon($aUpadteImage);
                        }
                    }
                }

                $sReturn = '?sMod=category_index';
                if($_POST['product_category_parent_id'] > 0)
                    $sReturn.= '&sAction=list_child&cat_id='.$_POST['product_category_parent_id'].'&cat_name='.$_POST['category_parent_name'].'&current_level='.$_POST['current_level'].'&cate_dir='.$_POST['cate_dir'];
                
                redir($sReturn);
            }
            break;
        }
    case 'list_child': {
            $oModelCat->setData('parent_category_id', $_GET['cat_id']);
            $oModelCat->setData('parent_category_name', $_GET['cat_name']);
            $oModelCat->setData('current_level', $_GET['current_level']);
            $oViewCat->displayManagerIndex($oModelCat);
            if($_GET['current_level']==0) {
                $_SESSION['path_cat']=null;
            }
            $oViewCat->displayListChildCategory($oModelCat);
            break;
        }
    default:
            $oViewCat->displayManagerIndex($oModelCat);
            $oViewCat->displayListCategoryNew($oModelCat);
            $oModelCat->unsetData('product_category_status');
            $_SESSION['category_change_igm_id']=null;
            $_SESSION['path_cat']=null;
        
}

$oViewCat->displayPage($oModelCat);
